package com.lovergreen.person.dao;

import com.lovergreen.person.model.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class PersonDaoImpl implements PersonDao{

    @Autowired
    private SessionFactory sessionFactory;
    public Session getCurrentSession() { return sessionFactory.getCurrentSession();	}
    @Override
    public Person getPerson(String email) {return (Person) getCurrentSession().get(Person.class, email); }
    @Override
    public List listPersons() {return getCurrentSession().createQuery("from Person").list();}
    @Override
    public void deletePerson(String email) { Person person = getPerson(email); getCurrentSession().delete(person); }
    @Override
    public void addPerson(Person person) { getCurrentSession().save(person); }
    @Override
    public void updatePerson(Person person) { getCurrentSession().update(person); }
    @Override
    public Person getPersonByUid(String confirmkey) {
        List list = getCurrentSession().createQuery("from Person where confirmkey=?").setParameter(0, confirmkey).list();
        return (Person)list.get(0);
    }
/*
    @Override
    public Person getPerson(String email) {
        List list = getCurrentSession().createQuery("from Person where email=?").setParameter(0, email).list();
        return (Person)list.get(0);
    }*/
}
